package cn.edu.buu.hospital.web;

import cn.edu.buu.hospital.entity.User;
import cn.edu.buu.hospital.service.AdminService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    // 添加index路径重定向到find方法，解决访问错误路径的问题
    @RequestMapping({"/index", "/"})
    public String index() {
        return "redirect:find";
    }

    @RequestMapping("/find")
    public String find(Model model){
        // 获取所有管理员列表
        List<User> adminList = adminService.findAllAdmins();
        // 将管理员列表添加到模型中
        model.addAttribute("adminList", adminList);
        return "backend/admin/list";//WEB-INF/backend/admin/list.jsp
    }

    //去往添加管理员页面
    @GetMapping("/toAdd")
    public String toAdd(){
        return "backend/admin/add";//WEB-INF/backend/admin/add.jsp
    }
    //执行添加管理员
    @PostMapping("/add")
    public String add(User admin, @RequestParam("file") MultipartFile file, HttpSession session) throws Exception{
        //转存文件
        ServletContext application =session.getServletContext();
        String serverRealPath= application.getRealPath("statics/images/adminHead/");
        System.out.println("serverRealPath ="+serverRealPath);
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename ="+originalFilename);
        String fileExtendsName = null;
        if (originalFilename != null) {
            fileExtendsName = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String fileName = UUID.randomUUID()+fileExtendsName;
        System.out.println("fileName = "+fileName);
        Path path = Paths.get(serverRealPath, fileName);
        file.transferTo(path);
        admin.setHeadPic(fileName);
        admin.setUserPassword("admin");
        admin.setUserType(3);

        //跳转，重定向
        return "redirect:find";
    }
    
    // 去往编辑管理员页面
    @GetMapping("/toEdit")
    public String toEdit(@RequestParam("userid") Integer userId, Model model) {
        // 根据ID查询管理员信息
        User admin = adminService.findById(userId);
        // 将管理员信息添加到模型中
        model.addAttribute("admin", admin);
        return "backend/admin/edit";//WEB-INF/backend/admin/edit.jsp
    }
    
    // 执行编辑管理员
    @PostMapping("/update")
    public String update(User admin, @RequestParam(value = "file", required = false) MultipartFile file, HttpSession session) throws Exception {
        // 如果有上传新头像，则处理文件
        if (file != null && !file.isEmpty()) {
            ServletContext application = session.getServletContext();
            String serverRealPath = application.getRealPath("statics/images/adminHead/");
            String originalFilename = file.getOriginalFilename();
            String fileExtendsName = null;
            if (originalFilename != null) {
                fileExtendsName = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            String fileName = UUID.randomUUID() + fileExtendsName;
            Path path = Paths.get(serverRealPath, fileName);
            file.transferTo(path);
            admin.setHeadPic(fileName);
        }
        
        // 执行更新操作
        adminService.update(admin);
        
        // 重定向到列表页面
        return "redirect:find";
    }
    
    // 执行删除管理员
    @GetMapping("/delete")
    public String delete(@RequestParam("userId") Integer userId) {
        // 执行删除操作
        adminService.deleteById(userId);
        // 重定向到列表页面
        return "redirect:find";
    }
}
